﻿#include "HardenRhino.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Enums.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../../../Shared/Data/Stat.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    HardenRhino::HardenRhino(MonsterInfo *info) : MonsterObject(info)
    {

    }

    bool HardenRhino::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }

        return getTarget()->getCurrentLocation() != getCurrentLocation() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);
    }

    void HardenRhino::ProcessSearch()
    {
        if (getEnvir()->getTime() < SearchTime)
        {
            return;
        }
        if (Master != nullptr && (Master->getPMode() == PetMode::MoveOnly || Master->getPMode() == PetMode::None))
        {
            return;
        }

        SearchTime = getEnvir()->getTime() + SearchDelay;

        if (getTarget() == nullptr || getEnvir()->Random->Next(3) == 0)
        {
            FindTarget();
        }

        if (getTarget() != nullptr && !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 3) && getEnvir()->Random->Next(3) == 0)
        {
            setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

            S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);

            DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 1500, {getTarget()});
            ActionList.push_back(action);

            ActionTime = getEnvir()->getTime() + 1500;
            MoveTime = getEnvir()->getTime() + 1500;

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void HardenRhino::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        ShockTime = 0;

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        S::ObjectAttack *tempVar = new S::ObjectAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        tempVar->Type = 0;
        Broadcast(tempVar);

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            return;
        }

        DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::ACAgility});
        ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void HardenRhino::CompleteRangeAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        Dash(target);

        MoveTo(target->getCurrentLocation());
        ActionTime = getEnvir()->getTime() + 300;
    }

    void HardenRhino::Dash(MapObject *target)
    {
        auto dist = Functions::MaxDistance(getCurrentLocation(), target->getCurrentLocation());

        auto travelled = 0;

        if (dist > 2)
        {
            auto location = getCurrentLocation();

            for (int i = 0; i < dist; i += 2)
            {
                if (Functions::MaxDistance(getCurrentLocation(), target->getCurrentLocation()) <= 2)
                {
                    break;
                }

                setDirection(Functions::DirectionFromPoint(getCurrentLocation(), target->getCurrentLocation()));

                location = Functions::PointMove(location, getDirection(), 2);

                if (!getCurrentMap()->ValidPoint(location))
                {
                    break;
                }

                getCurrentMap()->GetCell(getCurrentLocation())->Remove(this);
                RemoveObjects(getDirection(), 1);

                setCurrentLocation(location);
                travelled++;

                S::ObjectRun *tempVar = new S::ObjectRun();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = location;
                Broadcast(tempVar);

                getCurrentMap()->GetCell(getCurrentLocation())->Add(this);
                AddObjects(getDirection(), 1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
        }
    }
}
